package com.lzs.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lzs.PO.Menu;
import com.lzs.PO.Page;
import com.lzs.PO.Role;
import com.lzs.Service.MenuService;
import com.lzs.Service.RoleService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/role_add")

	public String addRole(Model model) {
		List<Menu> menus = this.menuService.selectMenus();
		model.addAttribute("menus", menus);
		return "/role/role_add";
	}

	@ResponseBody
	@RequestMapping(value = "/role_add_action")
	public Msg addRoleAction(String rolename, Integer roleid, String[] menuids) {
		try {
			Role role = new Role();
			role.setRoleid(roleid);
			role.setRolename(rolename);
			this.roleService.addRole(role);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roleid", roleid);
			map.put("menuids", menuids);
			System.out.println(map.get("menuids"));
			this.roleService.addRoleWithMenuids(map);
			return Msg.success();
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail();
		}
	}

	@RequestMapping(value = "/role_list")
	public String roleList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Role> page = this.roleService.findRolePagelist(keyword, pageNum, pageSize);

		model.addAttribute("page", page);
        
		return "/role/role_list";
	}

	@ResponseBody
	@RequestMapping(value = "/role_check")
	public Msg checkrole(@RequestParam("roleid") Integer roleid, HttpServletRequest request) {
		roleid = Integer.parseInt(request.getParameter("roleid"));
		boolean b = this.roleService.checkrole(roleid);
		if (b) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}

	@RequestMapping(value = "/role_update")
	public String roleUpdate(@RequestParam("roleid") Integer roleid, Model model, HttpServletRequest request) {
		roleid = Integer.parseInt(request.getParameter("roleid"));
		Role role = this.roleService.selectRoleMenuById(roleid);
		System.out.println(role);
		model.addAttribute("role", role);
		return "/role/role_update";
	}

	@ResponseBody
	@RequestMapping(value = "/role_update_action")
	public Msg roleUpdateAction(String rolename, Integer roleid, @RequestParam("menuids") String[] menuids) {
		try {
			Role role = new Role();
			role.setRoleid(roleid);
			role.setRolename(rolename);
			this.roleService.updateRole(role);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roleid", roleid);
			map.put("menuids", menuids);
			System.out.println("选中的角色编号" + map.get("roleid"));
			System.out.println("选中的节点编号" + menuids);
			this.roleService.updateRoleMenu(map);
			return Msg.success();
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/menudata_loadbyroleid")
	public Object loaddata(Integer roleid) {
		// 构造顶级节点
		Menu parent = new Menu();
		parent.setMenuid("0");
		// 调用方法递归查询权限数据
		this.menuService.queryChildMenus(parent, roleid);
		System.out.println("节点：" + parent.getChildren());
		System.out.println("roleid:" + roleid);
		return parent.getChildren();
	}
    @ResponseBody
	@RequestMapping(value = "/role_delete")
	public Msg deleteRoleMenuById(Integer roleid) {
        try {
        	
        	Map<String, Object> map = new HashMap<String, Object>();
    		map.put("roleid", roleid);

    		this.roleService.deleteRoleMenuById(map);
    		this.roleService.deleteRoleById(map);
    		System.out.println(map.get("roleid"));
    		return Msg.success();
        }catch(Exception e) {
        	e.printStackTrace();
        	return Msg.fail();
        }		
	}
}
